﻿using System;
using System.Collections;
using System.Security.Cryptography;
using System.IO;
using System.Text;

namespace ScoreHelper.Properties {
    
    
    // 通过此类可以处理设置类上的特定事件:
    // 在更改某个设置的值之前将引发 SettingChanging 事件。
    // 在更改某个设置的值之后将引发 PropertyChanged 事件。
    // 在加载设置值之后将引发 SettingsLoaded 事件。
    // 在保存设置值之前将引发 SettingsSaving 事件。
    internal sealed partial class Settings {

        private static byte[] DesKey = new byte[] { 39, 67, 97, 111, 104, 97, 105, 39 }; // 'Caohai'

        public Settings() {
            // // 若要为保存和更改设置添加事件处理程序，请取消注释下列行:

            this.SettingsLoaded += SettingsLoadedEventHandler;

            //this.SettingChanging += this.SettingChangingEventHandler;

            this.SettingsSaving += this.SettingsSavingEventHandler;
            
        }

        private void SettingsLoadedEventHandler(object sender, System.Configuration.SettingsLoadedEventArgs e)
        {
            // 对密码解密
            // this.Password = Encrypt(this.Password, false);
        }
        
        private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) {
            // 在此处添加用于处理 SettingChangingEvent 事件的代码。
        }
        
        private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) {
            // 对密码加密
            // this.Password = Encrypt(this.Password, true);
        }

        private string Encrypt(string strInput, bool bEncrypt)
        {
            DES enProvider;
            MemoryStream ms = null;
            CryptoStream cs = null;

            byte[] output;
            byte[] input;
            try
            {
                enProvider = DES.Create();
                enProvider.Key = DesKey;
                enProvider.Mode = CipherMode.ECB;
                enProvider.Padding = PaddingMode.None;
                ms = new MemoryStream();
                if (bEncrypt) // 加密
                {
                    //空格补齐8的倍数
                    int iLen = strInput.Length - strInput.Length % 8 + 8;
                    strInput = strInput.PadRight(iLen, ' ');
                    input = UTF8Encoding.UTF8.GetBytes(strInput);
                    cs = new CryptoStream(ms, enProvider.CreateEncryptor(), CryptoStreamMode.Write);
                }
                else // 解密
                {
                    input = Convert.FromBase64String(strInput);
                    cs = new CryptoStream(ms, enProvider.CreateDecryptor(), CryptoStreamMode.Write);
                }
                cs.Write(input, 0, input.Length);
                cs.FlushFinalBlock();
                output = ms.ToArray();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (cs != null)
                    cs.Close();
                if (ms != null)
                    ms.Close();
                cs = null;
                ms = null;
            }
            if (bEncrypt)
            {
                return Convert.ToBase64String(output);
            }
            else
            {
                return UnicodeEncoding.UTF8.GetString(output).TrimEnd(new char[]{' '}); // 去除补充的空格
            }
        }
    }
}
